<template>
<div class="basic-setting-content pl16 pr16">
    <!--基本信息-->
    <div class="common-form">{{$t('product.basic_info')}}</div>
    <el-form-item
      :label="$t('product.product_name_label')"
      :rules="[{  required: true,  message: $t('product.input_product_name1')}]"
      prop="model.productName"
    >
      <el-input v-model="form.model.productName" class="max-w460"></el-input>
    </el-form-item>
    <el-form-item :label="$t('product.product_code_label')"
      ><el-input v-model="form.model.productNo" class="max-w460"></el-input
    ></el-form-item>
    <el-form-item
      :label="$t('product.product_category_label')"
      :rules="[{  required: true,  message: $t('product.selected_product_category')}]"
      prop="model.categoryId"
    >
      <el-select
        v-model="form.model.categoryId"
        filterable
        style="width: 460px"
      >
        <template v-for="cat in form.category" :key="cat.categoryId">
          <el-option :value="cat.categoryId" :label="cat.name"></el-option>
          <template v-for="two in cat.children" :key="two.categoryId">
            <el-option
              :value="two.categoryId"
              :label="two.name"
              style="padding-left: 30px"
            ></el-option>
          </template>
        </template>
      </el-select>
    </el-form-item>
    <el-form-item :label="$t('product.sale_status_label')">
      <el-radio-group v-model="form.model.productStatus">
        <el-radio :label="10">{{$t('product.publish_now')}}</el-radio>
        <el-radio :label="20">{{$t('product.move_to_warehouse')}}</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item :label="$t('product.pre_sale_label')">
      <el-radio-group v-model="form.model.isPreview">
        <el-radio :label="1">{{$t('product.enable')}}</el-radio>
        <el-radio :label="0">{{$t('product.disable')}}</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item
      v-if="form.model.isPreview == 1"
      :label="$t('product.pre_sale_start_time')"
      :rules="[{  required: true,  message: $t('product.select_pre_sale_time')}]"
      prop="model.previewTime"
    >
      <el-date-picker
        v-model="form.model.previewTime"
        type="datetime"
        value-format="YYYY-MM-DD HH:mm:ss"
        :placeholder="$t('product.select_date')"
      ></el-date-picker>
    </el-form-item>
    <el-form-item
      :label="$t('product.product_image_label')"
      :rules="[{  required: true,  message: $t('product.upload_product_image')}]"
      prop="model.image"
    >
      <div class="draggable-list">
        <draggable class="wrapper" v-model="form.model.image">
          <template #item="{ element, index }">
            <div class="item">
              <img v-img-url="element.filePath" />
              <a
                href="javascript:void(0);"
                class="delete-btn"
                @click.stop="deleteImg(index)"
              >
                <el-icon><CloseBold /></el-icon>
              </a>
            </div>
          </template>
        </draggable>
        <div
          class="item img-select"
          @click="openProductUpload('image', 'image')"
        >
          <el-icon><Plus /></el-icon>
        </div>
      </div>
      <div class="gray9">{{$t('product.image_size_tip_750')}}</div>
    </el-form-item>
    <el-form-item :label="$t('product.video_type_label')">
      <el-radio-group v-model="form.model.videoType">
        <el-radio :label="0">{{$t('product.upload_video')}}</el-radio>
        <el-radio :label="1">{{$t('product.video_url')}}</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item
      :label="$t('product.video_url_label')"
      prop="model.videoLink"
      v-if="form.model.videoType == 1"
    >
      <el-input v-model="form.model.videoLink" class="max-w460"></el-input>
      <div class="gray">{{$t('product.video_suggestion')}}</div>
    </el-form-item>
    <el-form-item :label="$t('product.product_video_label')" v-if="form.model.videoType == 0">
      <el-row>
        <div class="draggable-list">
          <div
            class="item img-select"
            v-if="form.model.videoId == 0"
            @click="openProductUpload('video', 'video')"
          >
            <el-icon><Plus /></el-icon>
          </div>
          <div v-if="form.model.videoId != 0">
            <video
              width="150"
              height="150"
              :src="form.model.videoFilePath"
              :autoplay="false"
              controls
            > {{$t('product.video_tag_not_supported')}} 

</video>
            <div>
              <el-button icon="Picture" @click="delVideo">{{$t('product.delete_video')}}</el-button>
            </div>
          </div>
        </div>
      </el-row>
    </el-form-item>
    <el-form-item :label="$t('product.video_cover_label')">
      <el-row>
        <div class="draggable-list">
          <div
            class="item img-select"
            v-if="form.model.posterId == 0"
            @click="openProductUpload('image', 'poster')"
          >
            <el-icon><Plus /></el-icon>
          </div>
          <div
            v-if="form.model.posterId != 0"
            class="item"
            @click="openProductUpload('image', 'poster')"
          >
            <img :src="form.model.posterFilePath" width="100" height="100" />
          </div>
        </div>
      </el-row>
      <div class="gray9">{{$t('product.video_cover_tip_750')}}</div>
    </el-form-item>
    <el-form-item :label="$t('product.product_highlight')">
      <el-input
        type="textarea"
        v-model="form.model.sellingPoint"
        class="max-w460"
      ></el-input>
    </el-form-item>

    <!--商品图片组件-->
    <Upload
      v-if="isProductUpload"
      :config="config"
      :isupload="isProductUpload"
      @returnImgs="returnProductImgsFunc"
      >{{$t('product.upload_image')}}</Upload
    >
  </div>
</template>

<script>
import Upload from "@/components/file/Upload.vue";
import draggable from "vuedraggable";
export default {
  components: {
    Upload,
    draggable
  },
  data() {
    return {
      isProductUpload: false,
      config: {},
      file_name: "image"
    };
  },
  inject: ["form"],
  created() {},
  methods: {
    /*打开上传图片*/
    openProductUpload: function (file_type, file_name) {
      this.file_name = file_name;
      if (file_type == "image") {
        this.config = {
          total: 9,
          fileType: "image"
        };
      } else {
        this.config = {
          total: 1,
          fileType: "video"
        };
      }
      this.isProductUpload = true;
    },
    /*上传商品图片*/
    returnProductImgsFunc(e) {
      if (e != null) {
        if (this.file_name == "video") {
          this.form.model.videoId = e[0].fileId;
          this.form.model.videoFilePath = e[0].filePath;
        } else if (this.file_name == "image") {
          let imgs = this.form.model.image.concat(e);
          // 兼容后端
          for (let i = 0; i < imgs.length; i++) {
            if (typeof imgs[i].imageId == "undefined") {
              imgs[i].imageId = imgs[i].fileId;
            }
          }
          this.form.model["image"] = imgs;
        } else if (this.file_name == "poster") {
          this.form.model.posterId = e[0].fileId;
          this.form.model.posterFilePath = e[0].filePath;
        }
      }
      this.isProductUpload = false;
    },
    /*删除商品图片*/
    deleteImg(index) {
      this.form.model.image.splice(index, 1);
    },
    delVideo() {
      this.form.model.videoId = 0;
      this.form.model.video = {};
    }
  }
};
</script>

<style>
.edit_container {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  line-height: 20px;
  color: #2c3e50;
}

.ql-editor {
  height: 400px;
}

.draggable-list {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
}
.draggable-list .wrapper {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
}
.draggable-list .item {
  position: relative;
  width: 110px;
  height: 110px;
  margin-top: 10px;
  margin-right: 10px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid #dddddd;
}

.draggable-list .delete-btn {
  position: absolute;
  top: 0;
  right: 0;
  width: 16px;
  height: 16px;
  background: red;
  line-height: 16px;
  font-size: 16px;
  color: #ffffff;
  display: none;
}

.draggable-list .item:hover .delete-btn {
  display: block;
}

.draggable-list .item img {
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  max-height: 100%;
  max-width: 100%;
}

.draggable-list .img-select {
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px dashed #dddddd;
  font-size: 30px;
}

.draggable-list .img-select i {
  color: #409eff;
}
</style>

